import md5 from 'js-md5';
var JsonConvert = function (data) {
    var result = "";
    if (data != null) {
        for (var p in data) { //遍历json对象的每个key/value对,p为key
            result += p + "=" + data[p] + "&";
        }
        result = result.substr(0, result.length - 1);
    }
    return result;
}

/**
 * @name: Saiu
 * @msg:  密码加密，
 * @param {pwd}原密码 {salte}加盐
 * @return: 返回加密后的数据
 */
var DecPwd = function (pwd, salte) {
    var res = '';
    if (salte == '') {
        var md5byte = md5(StringToBytes(pwd));
        res = md5byte;
    } else {
        var md5byte = md5(StringToBytes(pwd + salte));
        res = md5byte;
    }
    return res;
};
/**
 * string 转 byte
 * @param str
 */
var StringToBytes = function (str) {
    var bytes = new Array();
    var len, c;
    len = str.length;
    for (var i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if (c >= 0x010000 && c <= 0x10FFFF) {
            bytes.push(((c >> 18) & 0x07) | 0xF0);
            bytes.push(((c >> 12) & 0x3F) | 0x80);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000800 && c <= 0x00FFFF) {
            bytes.push(((c >> 12) & 0x0F) | 0xE0);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000080 && c <= 0x0007FF) {
            bytes.push(((c >> 6) & 0x1F) | 0xC0);
            bytes.push((c & 0x3F) | 0x80);
        } else {
            bytes.push(c & 0xFF);
        }
    }
    return bytes;
};
var SortField = function (property, order) {
    return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        if (order == 'desc') {
            return value2 - value1;
        } else {
            return value1 - value2;
        }
    };
}
///时间过滤器
var dateFormat = {
    padLeftZero: function (str) {
        return ('00' + str).substr(str.length)
    },
    formatDate: function (date, fmt) {
        date.toLocaleString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        }
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                let str = o[k] + ''
                fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str))
            }
        }
        return fmt
    }
}

var dateAdd = function (interval, number, objDate) {
    var k = dateFormat.formatDate(objDate, 'yyyy-MM-dd hh:mm:ss');
    var d = new Date(k);
    var k = {
        'y': 'FullYear',
        'q': 'Month',
        'm': 'Month',
        'w': 'Date',
        'd': 'Date',
        'h': 'Hours',
        'n': 'Minutes',
        's': 'Seconds',
        'ms': 'MilliSeconds'
    };
    var n = {
        'q': 3,
        'w': 7
    };
    eval('d.set' + k[interval] + '(d.get' + k[interval] + '()+' + ((n[interval] || 1) * number) + ')');
    return d;
}

var toTree = function (data) {
    let result = [];
    if (!Array.isArray(data)) {
        return result;
    }
    data.forEach((item) => {
        delete item.children;
    });
    let map = {};
    data.forEach((item) => {
        map[item.id] = item;
    });
    data.forEach((item) => {
        let parent = map[item.pid];
        if (parent) {
            item["IsChildren"]=false;
            (parent.children || (parent.children = [])).push(item);
        } else {
            item["IsChildren"]=true;
            result.push(item);
        }
    });
    return result;
}

export {
    DecPwd,
    StringToBytes,
    dateFormat,
    SortField,
    toTree,
    dateAdd
};